/*
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License");  you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * http//www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 *
 * The Original Code is ART Ontology API.
 *
 * The Initial Developer of the Original Code is University of Roma Tor Vergata.
 * Portions created by University of Roma Tor Vergata are Copyright (C) 2007.
 * All Rights Reserved.
 *
 * ART Ontology API was developed by the Artificial Intelligence Research Group
 * (art.uniroma2.it) at the University of Roma Tor Vergata
 * Current information about the ART Ontology API can be obtained at 
 * http//art.uniroma2.it/owlart
 *
 */

/*
 * Contributor(s): Armando Stellato stellato@info.uniroma2.it
 */
package it.uniroma2.art.owlart.filter;

import it.uniroma2.art.owlart.exceptions.ModelAccessException;
import it.uniroma2.art.owlart.model.ARTResource;
import it.uniroma2.art.owlart.model.ARTURIResource;
import it.uniroma2.art.owlart.models.SKOSModel;
import it.uniroma2.art.owlart.navigation.ARTURIResourceIterator;

import com.google.common.base.Predicate;

/**
 * This predicate allows developers to retrieve concepts which could be used as roots for a concept tree built
 * upon model <code>model</code><br/>
 * This filter can be used instead of looking over triples containing the predicate
 * <code>skos:topConceptOf</code> for a given scheme. In this case, all the concepts which have no broader
 * concepts are returned as root concepts
 *  
 * 
 * @author Armando Stellato
 */
public class RootConceptsPredicate implements Predicate<ARTURIResource> {

	SKOSModel repo;
	ARTResource[] graphs;

	public RootConceptsPredicate(SKOSModel model, ARTResource...graphs) {
		this.repo = model;
		this.graphs = graphs;
	}

	public boolean apply(ARTURIResource concept) {

		try {
			ARTURIResourceIterator it = repo.listBroaderConcepts(concept, false, true, graphs);
			if (!it.streamOpen())
				return true;
		} catch (ModelAccessException e) {
			return false;
		}

		return false;
	}

}
